#include <QDir>
#include <QSql>
#include <QDateTime>

#include "../../dataBase/connection.h"
#include "../gloab/globalobserver.h"
#include "widgetlogform.h"
#include "ui_widgetlogform.h"

using namespace  QsLogging;

WidgetLogForm::WidgetLogForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::WidgetLogForm)
{
    ui->setupUi(this);
    this->InitTreeView();
    logInit();
}

WidgetLogForm::~WidgetLogForm()
{
    delete ui;
}

//messag： level data time message
void WidgetLogForm::logtoDateBaseSlot(const QString &message, int level)
{
    QString messTemp = message;
    messTemp = messTemp.replace("  ", " ");

    QStringList strList = messTemp.split(" ");
    QString messageLevel = strList[0];
    QString messageDateTime = strList[1].append(strList[2]);
    QString messageFile = strList[3];
    QString messageFunction = strList[5];
    QString messageLine = strList[7];
    QString messageContent;
    for(int i = 8; i < strList.size(); ++i){
        messageContent.append(strList[i]);
    }


    QString strSql = QString("INSERT INTO \"log\"(\"level\", \"time\",\"file\",\"line\",\"func\",\"message\")"
                             " VALUES('%1','%2','%3','%4','%5','%6');").arg(messageLevel).arg(messageDateTime)
                         .arg(messageFile).arg(messageLine).arg(messageFunction).arg(messageContent);

    Connection::getInstance()->update(strSql);
}


void WidgetLogForm::InitTreeView()
{
    ui->tableView->setWindowTitle("数据列表");
    ui->tableView->setWindowIconText("数据列表");
    m_model.setRowCount(0);
    m_model.setColumnCount(6);
    m_model.setHorizontalHeaderLabels(QStringList() << "时间"
                                                    << "等级"<<"事件"
                                                    <<"函数"<<"行号");

    QHeaderView *headerView = ui->tableView->horizontalHeader();
    // 设置样式表，包括背景颜色和字体样式
    headerView->setSectionResizeMode(QHeaderView::ResizeMode::Stretch);
    headerView->setStyleSheet("QHeaderView::section { background-color: #8da1ff; color: white }");
    headerView->setTextElideMode(Qt::TextElideMode::ElideMiddle);

    ui->tableView->setModel(&m_model);
}

void WidgetLogForm::logInit()
{

    //1.设置日志级别
    Logger& logger = Logger::instance();
    logger.setLoggingLevel(QsLogging::InfoLevel);

    //
    const QString sLogPath(QDir(QCoreApplication::applicationDirPath()).filePath("log.txt"));

    // 2. 添加三个destination
    DestinationPtr fileDestination(DestinationFactory::MakeFileDestination(
        sLogPath, EnableLogRotation, MaxSizeBytes(512*1024), MaxOldLogCount(2)));
    DestinationPtr debugDestination(DestinationFactory::MakeDebugOutputDestination());

    DestinationPtr sigsSlotDestination(DestinationFactory::MakeFunctorDestination(this, SLOT(logtoDateBaseSlot(QString,int))));

    //logger.addDestination(debugDestination);
    logger.addDestination(sigsSlotDestination);
    logger.addDestination(fileDestination);

    QLOG_INFO()<<CALLPOINTINFO<<"日志初始化成功";

}
